//
//  LWHomeTabVC.swift
//  Swift_Weibo_HLW
//
//  Created by huanglinwang on 16/9/19.
//  Copyright © 2016年 Linwang. All rights reserved.
//

import UIKit
import YYModel
// MARK: -  重用标记
private let lwHomeTabCellIndentifier = "lwHomeTabCellIndentifier"

class LWHomeTabVC: LWVisitorTabVC {

    //对应的ViewModel对象
    fileprivate lazy var statusListViewModle:LWStatusListViewModel = LWStatusListViewModel()
    
    //  上拉加载
    fileprivate lazy var pullUpView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        view.color = UIColor.red
        return view
        
    }()
//    fileprivate lazy var pullDownView: LWRefreshControl = {
//        let view = LWRefreshControl()
//        //  添加下拉刷新事件
//        view.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
//        return view
//        
//    }()
    //  自定义下拉刷新控件
    fileprivate lazy var pullDownCtr: LWRefreshControl = {
        let view = LWRefreshControl()
        view.addTarget(self, action: #selector(pullDownRefreshing), for: .valueChanged)
        return view
    }()
    //  下拉刷新操作
    @objc private func pullDownRefreshing() {
        print("下拉刷新")
        loadData()
    }
    //  tipLabel
    fileprivate lazy var tipLabel: UILabel = {
        let label = UILabel(textColor: UIColor.white, fontSize: 12)
        label.backgroundColor = UIColor.orange
        label.textAlignment = .center
        label.text = "没有加载到最新的微博数据"
        label.isHidden = true

        return label
        
    }()


    
    // MARK: -  懒加载数组
    lazy var statusList:[LWStatus] = [LWStatus]()
    
    override func viewDidLoad() {
       
        super.viewDidLoad()
     
        if !isLogin{
            
            visitorView?.updateVisitorInfo(message: nil, imageName: nil)
            
        }else{
            setupUI()

            setupTabView()
            //表示登陆加载数据
            loadData()
        }
    }
    //  添加控件设置约束
    private func setupUI() {
        
        if let nav = self.navigationController {
            //  把tipLabel添加到导航栏下面
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            //  设置其与导航栏底部对象
            //  nav.navigationBar.frame.maxY获取最大y轴 -> swift 3.0
            
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: nav.navigationBar.width, height: 35)

        }
        
        
    }

    //  开启tip动画
    private func startAnimation(message: String) {
        
        //  当前tipLabel如果显示表示正在执行动画,那么不需要再次执行
        if tipLabel.isHidden == false {
            return
        }
        
        
        tipLabel.text = message
        //  显示label
        tipLabel.isHidden = false
        
        UIView.animate(withDuration: 1, animations: {
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y: self.tipLabel.height)
            
        }) { (_) in
            UIView.animate(withDuration: 1, animations: {
                self.tipLabel.transform = CGAffineTransform.identity
                }, completion: { (_) in
                    self.tipLabel.isHidden = true
            })
        }
        
        
        
    }
    

    // MARK: -  tabView 的相关操作
    private func setupTabView(){
        
        tableView.register(LWHomeTabCell.self, forCellReuseIdentifier: lwHomeTabCellIndentifier)
        
        //设置行高
        tableView.rowHeight = UITableViewAutomaticDimension;
        //预估行高
        tableView.estimatedRowHeight = 200
        
        //  去掉分割线
        tableView.separatorStyle = .none
        
        
        tableView.tableFooterView = pullUpView
        
        tableView.addSubview(pullDownCtr)


    }
    private func endRefreshing() {
        //  结束刷新
        pullUpView.stopAnimating()
        //  结束下拉刷新
        pullDownCtr.endRefreshing()

        
    }

    
    //获取数据
    func loadData() -> () {
        
        statusListViewModle.loadData(isPullup: pullUpView.isAnimating) { (isSuccess, message) in
            
            if !self.pullUpView.isAnimating {
                self.startAnimation(message: message)
            }
            
            self.endRefreshing()
            if isSuccess{
                
                self.tableView.reloadData()
                
            }
        }
    }
    
    
}
// MARK: - dataSource
extension LWHomeTabVC{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModle.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: lwHomeTabCellIndentifier, for: indexPath) as! LWHomeTabCell
        
        //会崩溃 -> 因为 "\(statusList[indexPath.row].user 是一个字典,不能直接把字典给user对象
//        cell.textLabel?.text = "\(statusList[indexPath.row].user?.screen_name)"
        
        //获取Cell对应的viewModel
        cell.statusViewModel = statusListViewModle.statusList[indexPath.row]
        // MARK: - Cell的选中状态
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        
        return cell
    }
    
    //  将要显示cell的时候判断是否是最后一个cell
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        if indexPath.row == statusListViewModle.statusList.count - 1 && !pullUpView.isAnimating {
            pullUpView.startAnimating()
            print("已经滚动到最后一个cell了")
            loadData()
        }
    }

    
}

